package com.longbo.leaf.vo;

import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.longbo.leaf.common.Consts;
import com.longbo.leaf.portal.model.Permission;
import com.longbo.leaf.portal.model.Role;
import com.longbo.leaf.portal.model.User;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class UserPrincipal implements UserDetails {

    /**
     * 主键
     */
    private Long id;

    /**
     * 用户名
     */
    private String username;

    /**
     * 密码
     */
    @JsonIgnore
    private String password;

    /**
     * 昵称
     */
    private String nickname;

    /**
     * 真实名
     */
    private String realName;

    /**
     * 手机
     */
    private String phone;

    /**
     * 邮箱
     */
    private String email;

    /**
     * 地址
     */
    private String address;

    /**
     * 性别,男-1,女-2
     */
    private Integer gender;

    /**
     * 爱好
     */
    private String hobby;

    /**
     * 状态,启用-1,禁用-0
     */
    private  Integer status;

    /**
     * 创建时间
     */
    private LocalDateTime gmtCreated;

    /**
     * 修改时间
     */
    private LocalDateTime gemModified;

    /**
     * 用户角色列表
     */
    private List<String> roles;

    /**
     * 用户权限列表
     */
    private Collection<? extends GrantedAuthority> authorities;


    public static UserPrincipal create(User user, List<Role> roles,List<Permission> permissions){
         List<String> roleNames = roles.stream()
                 .map(Role::getName)
                 .collect(Collectors.toList());
         List<GrantedAuthority> authorities = permissions.stream()
                 .filter(permission -> StrUtil.isNotBlank(permission.getAuthority()))
                 .map(permission -> new SimpleGrantedAuthority(permission.getAuthority()))
                 .collect(Collectors.toList());
         return new UserPrincipal(user.getId(),user.getUsername(),user.getPassword(),user.getNickname(),user.getRealName(),
                 user.getPhone(),user.getEmail(),user.getAddress(),user.getGender(),user.getHobby(),user.getStatus(),
                 user.getGmtCreated(),user.getGmtModified(),roleNames,authorities);
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return authorities;
    }

    @Override
    public String getPassword() {
        return password;
    }

    @Override
    public String getUsername() {
        return username;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return Objects.equals(this.status, Consts.ENABLE);
    }
}
